package com.scqkxx.gamebox.activity.activities.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scqkxx.gamebox.R;
import com.scqkzqtz.base.library.utils.DimensionUtil;


/**
 * Created by admin on 2018/8/10. 11点43分
 * name : ts
 * 用途： 封装的title
 */

public class TitleView extends RelativeLayout {
    private View mTitleView;
    //title 类型
    public static final int TITLE_TYPE_HOME = 1; //主页面title
    public static final int TITLE_TYPE_USER_HOME = 2; // 主页面的个人中心title
    public static final int TITLE_TYPE_SENOND_LEVEL = 3;// 二级页面的title

    //title 的监听类型
    public static final int CLICK_LIFT = 1; //商城
    public static final int CLICK_RIGHT = 2; // 客服
    public static final int CLICK_BACK = 3;// 返回
    public static final int CLICK_SETTING = 4;// 设置

    private int mTitleType;
    private String mTitleNmae;
    private TextView tv_lift; //左边的lift
    private TextView tv_right; //左边的lift
    private TextView tv_name; //title昵称
    private ImageView iv_setting; //设置图标
    private String liftName; //左边的标题
    private String rightName; //右边的标题
    private onTitleClickListener onTitleClickListener;

    public void setOnTitleClickListener(TitleView.onTitleClickListener onTitleClickListener) {
        this.onTitleClickListener = onTitleClickListener;
    }

    public void setmTitleType(int mTitleType) {
        this.mTitleType = mTitleType;
    }

    public void setTitleName(String titleName) {
        this.mTitleNmae = titleName;
    }

    //获取当前
    public int getmTitleType() {
        return mTitleType;
    }

    //左边的TextView
    public TextView getTv_lift() {
        return tv_lift;
    }

    //右边的TextView
    public TextView getTv_right() {
        return tv_right;
    }

    public ImageView getIv_setting() {
        return iv_setting;
    }


    //标题
    public TextView getTv_name() {
        return tv_name;
    }

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypedArray(attrs);
        init();
        initListener();
    }

    //设置监听
    private void initListener() {
        //设置
        iv_setting.setOnClickListener(v -> {
            if (onTitleClickListener != null) {
                onTitleClickListener.onTitleClick(CLICK_SETTING);
            }
        });
        //客服
        tv_right.setOnClickListener(v -> {
            if (onTitleClickListener != null) {
                onTitleClickListener.onTitleClick(CLICK_LIFT);
            }
        });
        //商城和返回的监听
        tv_lift.setOnClickListener(v -> {
            if (onTitleClickListener != null) {
                if (mTitleType == TITLE_TYPE_HOME || mTitleType == TITLE_TYPE_USER_HOME) {
                    onTitleClickListener.onTitleClick(CLICK_RIGHT);
                } else {
                    onTitleClickListener.onTitleClick(CLICK_BACK);
                }
            }
        });
    }


    //获取自定义参数
    private void initTypedArray(AttributeSet attrs) {
        @SuppressLint("CustomViewStyleable")
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TitleStyle);
        mTitleType = array.getInteger(R.styleable.TitleStyle_titleType, TITLE_TYPE_HOME);
        mTitleNmae = array.getString(R.styleable.TitleStyle_titleName);
        liftName = array.getString(R.styleable.TitleStyle_liftName);
        rightName = array.getString(R.styleable.TitleStyle_rightName);
        array.recycle();
    }

    //初始化
    @SuppressLint("InflateParams")
    private void init() {
        mTitleView = LayoutInflater.from(getContext()).inflate(R.layout.layout_title, null, false);
        tv_lift = mTitleView.findViewById(R.id.tv_lift);
        tv_right = mTitleView.findViewById(R.id.tv_right);
        tv_name = mTitleView.findViewById(R.id.tv_name);
        iv_setting = mTitleView.findViewById(R.id.iv_setting);
        addView(mTitleView, new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        switchTitleType(mTitleType);
    }

    //根据type来设置type
    protected void switchTitleType(int mTitleType) {
        switch (mTitleType) {
            case TITLE_TYPE_HOME:
                iv_setting.setVisibility(VISIBLE);
                tv_name.setVisibility(VISIBLE);
                if (!TextUtils.isEmpty(liftName)) {
                    tv_lift.setVisibility(VISIBLE);
                    tv_lift.setText(liftName);
                } else {
                    tv_lift.setVisibility(GONE);
                }
                if (!TextUtils.isEmpty(rightName)) {
                    tv_right.setVisibility(VISIBLE);
                    tv_right.setText(rightName);
                } else {
                    tv_right.setVisibility(GONE);
                }
                //设置tv_rigth 的
                setLayoutParams(tv_right, 8);
                break;
            case TITLE_TYPE_USER_HOME:
                iv_setting.setVisibility(VISIBLE);
                tv_name.setVisibility(VISIBLE);
//                tv_lift.setVisibility(VISIBLE);
                tv_right.setVisibility(VISIBLE);
                if (!TextUtils.isEmpty(liftName)) {
                    tv_lift.setVisibility(VISIBLE);
                    tv_lift.setText(liftName);
                } else {
                    tv_lift.setVisibility(INVISIBLE);
                }
//                tv_lift.setText(!TextUtils.isEmpty(liftName) ? liftName :"商城");
                tv_right.setText("");
                setLayoutParams(tv_right, 16);
                break;
            case TITLE_TYPE_SENOND_LEVEL:
                tv_name.setVisibility(VISIBLE);
                tv_lift.setVisibility(VISIBLE);
                tv_right.setVisibility(INVISIBLE);
                iv_setting.setVisibility(VISIBLE);
                setLayoutParams(tv_right, 8);
                setLiftIcon(tv_lift);
                break;
        }
        tv_right.setVisibility(INVISIBLE); //客服影藏掉，暂时不开放
        tv_right.setEnabled(false);
        tv_name.setText(!TextUtils.isEmpty(mTitleNmae) ? mTitleNmae : "");
    }

    //设置Params
    protected void setLayoutParams(View view, int dp) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.setMargins(0, 0, DimensionUtil.dip2px(getContext(), dp), 0);
        tv_right.setLayoutParams(params);
    }

    //设置lift的iocn
    protected void setLiftIcon(TextView tv_lift) {
        tv_lift.setText("   ");
        Drawable drawableLeft = getResources().getDrawable(
                R.mipmap.icon_back);
        tv_lift.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                null, null, null);
    }

    //设置lift的iocn
    public void setLeftIcon(int res) {
        tv_lift.setText("   ");
        Drawable drawableLeft = getResources().getDrawable(
                res);
        tv_lift.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                null, null, null);
    }

    //设置lift的iocn
    public void setRightIcon(int res) {
        tv_right.setText("   ");
        Drawable drawableLeft = getResources().getDrawable(
                res);
        tv_right.setCompoundDrawablesWithIntrinsicBounds(null,
                null, drawableLeft, null);
    }

    /**
     * title 的监听
     */
    public interface onTitleClickListener {
        //        void onSettingClick();
//        //客服的监听
//        void onServiceClick();
//        //返回的监听
//        void onBackClick();
//        //商城的回调
//        void onShoppingClick();
        // 1.商城  2.客服  3.返回 4.设置
        void onTitleClick(int mTitleType);
    }

}
